var serialDevices = document.getElementById("serial_devices");
var btnOpen = document.getElementById("open");
var btnClose = document.getElementById("close");
var btnSend = document.getElementById("send");
var btnRefresh = document.getElementById("refresh");
var connection = null;

var init = function() {
    if (!serial_lib)
        throw "You must include serial.js before";

    enableOpenButton(true);
    btnOpen.addEventListener("click", openDevice);
    btnClose.addEventListener("click", closeDevice);
    btnSend.addEventListener("click", onSend);
    btnRefresh.addEventListener("click", refreshPorts);
    refreshPorts();
};

var refreshPorts = function() {
    while (serialDevices.options.length > 0)
        serialDevices.options.remove(0);

    serial_lib.getDevices(function(items) {
        console.log("got " + items.length + " ports");
        for (var i = 0; i < items.length; ++i) {
            var path = items[i].path;
            serialDevices.options.add(new Option(path, path));
            if (i === 1 || /usb/i.test(path) && /tty/i.test(path)) {
                serialDevices.selectionIndex = i;
                console.log("auto-selected " + path);
            }
        }
    });
};


var enableOpenButton = function(enable) {
    btnOpen.disabled = !enable;
    btnClose.disabled = enable;
};

var onOpen = function(newConnection) {
    if (newConnection === null) {
        logError("Failed to open device.");
        return;
    }
    connection = newConnection;
    connection.onReceive.addListener(onReceive);
    connection.onError.addListener(onError);
    connection.onClose.addListener(onClose);
    console.log("Device opened.");
    enableOpenButton(false);
    console.log("Connected")
};

var openDevice = function() {
    var selection = serialDevices.selectedOptions[0];
    if (!selection) {
        logError("No port selected.");
        return;
    }
    var path = selection.value;
    console.log("Connecting",path);
    enableOpenButton(false);
    serial_lib.openDevice(path, onOpen);
};

var closeDevice = function() {
    if (connection !== null) {
        connection.close();
    }
};


var onClose = function(result) {
    connection = null;
    enableOpenButton(true);
    console.log("Hover here to connect");
}
var onError = function(errorInfo) {
    if (errorInfo.error !== 'timeout') {
        console.log("Fatal error encounted. Dropping connection.");
        closeDevice();
    }
};
var onReceive = function(data) {
    console.log(data);
    disRecieve = document.getElementById("disRecieve");
    disRecieve.value = disRecieve.value + data;

}

var onSend = function() {
    disSend = document.getElementById("disSend"); 
    disSend.value=disSend.value.replace(/(^\s*)|(\s*$)/g,"");
    console.log(disSend.value);
    if (disSend.value === ''||disSend.value==" ") {
        console.log("发送不能为空");
        return;
    }
    console.log(disSend.value);
    sendSerial(disSend.value);
}
var sendSerial = function(message) {
    console.log(message);
    if (connection === null) {
        return;
    }
    if (!message) {
        logError("Nothing to send!");
        return;
    }
    console.log(message);
    connection.send(message);
};

init();